package gms.actions.um;

import gms.actions.SuperAction;
import gms.entities.um.Role;
import gms.entities.um.RolePermission;
import gms.service.UserManagementService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

public class RoleManagementAction extends SuperAction implements
		ModelDriven<Role> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Role role;
	private Integer[] rpList;


	@Autowired
	private UserManagementService userManagementService;
	
	public String roleManage(){
		List<Role> role_list = userManagementService.getAllRole();
		List<RolePermission> rp_list = userManagementService.getAllPermissions();
		session.setAttribute("role_list", role_list);
		session.setAttribute("rp_list", rp_list);
		System.out.println(rp_list);
		System.out.println(role_list);
		return "roleManage";
	}
	
	public String roleAdd(){
		userManagementService.addRole(role.getRoleName(), rpList);
		List<Role> role_list = userManagementService.getAllRole();
		session.setAttribute("role_list", role_list);
		return "roleManage";
	}

	public String roleModifi(){
		
		userManagementService.modifiRole(role.getRoleName(), rpList);
		List<Role> role_list = userManagementService.getAllRole();
		session.setAttribute("role_list", role_list);
		return "roleManage";
	}
	
	
	public String roleDelete(){
		Integer roleId = Integer.parseInt(request.getParameter("deleteRoleId"));
		
		userManagementService.deleteRole(roleId);
		
		List<Role> role_list = (List<Role>)session.getAttribute("role_list");
		int tem = 0;
		for(Role u : role_list){
			if(roleId==u.getRoleId())
				break;
			tem++;
		}
		
		role_list.remove(tem);
		session.setAttribute("role_list", role_list);
		return "roleManage";
	}
	
	
	@Override
	public Role getModel() {
		// TODO Auto-generated method stub
		if (role == null)
			role = new Role();
		return role;
	}

	public Integer[] getRpList() {
		return rpList;
	}
	
	public void setRpList(Integer[] rpList) {
		this.rpList = rpList;
	}
}
